home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1019 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  94 lines

  1. Path: s02.pavilion.co.uk!usenet
  2. From: AJRobb@pavilion.co.uk (Andy J Robb)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: To malloc (new) or not to malloc?  Clarification. Please ignore previous post.
  5. Date: Wed, 10 Jan 1996 21:44:05 GMT
  6. Organization: Pavilion Internet plc
  7. Message-ID: <4d1bsf$28s@s02.pavilion.co.uk>
  8. References: <4ctvk3$ort@maverick.tad.eds.com> <4cua5t$dm@maverick.tad.eds.com>
  9. NNTP-Posting-Host: poolb35.pavilion.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. fignet05.darrins@eds.com (Darrin Smith) wrote:
  13.  
  14. >Why is it that you can do something like the following:
  15.  
  16. >        char *x;
  17.  
  18. >        x="Some really long string with no particular meaning";
  19.  
  20. x now POINTS to this string it does not contain any data itself.
  21.  
  22. >and have no problems, but can't (safely) do something like this:
  23.  
  24. >        struct st1{char one[10];
  25. >                   char two[20];
  26. >                   char three[10];
  27. >                  };
  28.  
  29. >        st1 *sptr;      //or struct st1 *sptr in C instead of C++
  30.  
  31. You now have an unitialized pointer - reusing old space on the stack
  32. for itself.  It does not point at any data space.  Chances are it will
  33. be pointing somewhere into your program!  By the way, this is C++, in
  34. C you need:
  35.  
  36. struct st1 *sptr;
  37.  
  38. >    fread(sptr,sizeof(st1),1,infile);
  39.  
  40. If sptr is pointing to somewhere outside your data space, a good
  41. operating system (not DOS) will immediately terminate the program.
  42.  
  43. >This caused a program I was trying to debug to crash.  When I allocated 
  44. >memory for sptr (I used sptr=new st1; but I suppose malloc would have done 
  45. >just as well) it worked fine.                   
  46.  
  47.  
  48. >What is going on here?  Why isn't memory set aside for st1 *sptr just as 
  49. >it is for char *x?
  50.  
  51. No memory was set asside for *x, it was made to point to a string.  In
  52. your case, the string was effectively constant - you should have
  53. declared x as:
  54.  
  55. const char *x = "your long string . . . ";
  56.  
  57. This will try to prevent you program from overwriting data that may be
  58. in the text (code) area.  This is a common method of reducing memory
  59. requirement and speeding program startup - all concurrent copies of
  60. the program can use the same memory for such strings.
  61.  
  62. >Before I did the new (or malloc) it seemed as though 
  63. >my program was getting written over!
  64.  
  65. new is a C++ operator - it does not work in ANSI or K&R C.
  66.  
  67. malloc() requests memory to be assigned (like new).
  68.  
  69. struct st1 *sptr = malloc(sizeof(struct st1));
  70.  
  71. fread(sptr, sizeof(struct st1), 1, infile);
  72.  
  73. This will sort of work - however there are no guarantees - best not do
  74. it.  Many compilers leave differing gaps within a struct between
  75. members.  Thus:
  76.  
  77. (sizeof(struct st1) == 40) may not be true.
  78.  
  79. Remember, the memory allocated by malloc stays allocated until free()
  80. (or realloc()) is called.
  81.  
  82. Regards,
  83. -----BEGIN PGP PUBLIC KEY BLOCK-----
  84. Version: 2.6.2i
  85.  
  86. mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  87. MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  88. B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  89. tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  90. IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  91. =/wVD
  92. -----END PGP PUBLIC KEY BLOCK-----
  93.  
  94.